"""
Case Type   : SQL语法
Case Name   : 表所有者为普通用户,对表执行truncate
Create At   : 2023/04/19
Owner       : cr13
Description :
    1.创建普通用户
    2.切换普通用户,建表并插入数据
    3.用户对表执行truncate
    4.查询表数据
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.查询为空
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.tb_name = 't_ddl_truncate_016'
        self.user = 'u_ddl_truncate_016'

    def test_truncate(self):
        text = "----step1:创建普通用户 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.user};create user {self.user} " \
            f"with password '{macro.COMMON_PASSWD}';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = "----step2:切换普通用户,建表并插入数据 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
            f"create table {self.tb_name}(id int);" \
            f"insert into {self.tb_name} values(1);"
        result = self.commsh.execut_db_sql(
            sql_cmd, f"-U {self.user} -W '{macro.COMMON_PASSWD}'")
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = "----step3:用户对表执行truncate expect:成功----"
        self.log.info(text)
        sql_cmd = f"truncate {self.tb_name};"
        result = self.commsh.execut_db_sql(
            sql_cmd, f"-U {self.user} -W '{macro.COMMON_PASSWD}'")
        self.log.info(result)
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = "----step4:查询表数据 expect:查询为空----"
        self.log.info(text)
        sql_cmd = f"select * from {self.tb_name};"
        result = self.commsh.execut_db_sql(
            sql_cmd, f"-U {self.user} -W '{macro.COMMON_PASSWD}'")
        self.log.info(result)
        self.assertEqual(str(result.splitlines()[-1]).strip(), '(0 rows)',
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step5:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name} cascade;" \
            f"drop user {self.user} cascade;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
